﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using Entities;

namespace QuanLyBilliards_Coffee
{
    public partial class FormTimKiemBan : Form
    {
        public FormTimKiemBan()
        {
            InitializeComponent();
        }

        private void FormTimKiemBan_Load(object sender, EventArgs e)
        {
            cbbKieuTimKiemBanCafe.SelectedIndex = 0;
            cbbLoaiBan.SelectedIndex = 0;
            lblTimbanCFTheoten.Visible = false;
            txtTenBanCFTim.Visible = false;
            lblTrangthaibanCFTim.Visible = false;
            rdbanCFDangban.Checked = true;
            rdbanCFDangban.Visible = false;
            rdbanCFDangranh.Visible = false;
            lblChonLoaiBan.Visible = false;
            rdBillards.Visible = false;
            rdCafe.Visible = false;
            cbbLoaiBan.Visible = false;
        }

        private void cbbKieuTimKiemBanCafe_SelectedIndexChanged(object sender, EventArgs e)
        {
            int chon = cbbKieuTimKiemBanCafe.SelectedIndex;
            switch (chon)
            {
                case 0:
                    lblTimbanCFTheoten.Visible = false;
                    txtTenBanCFTim.Visible = false;
                    lblTrangthaibanCFTim.Visible = false;
                    rdbanCFDangban.Visible = false;
                    rdbanCFDangranh.Visible = false;
                    lblChonLoaiBan.Visible = false;
                    rdBillards.Visible = false;
                    rdCafe.Visible = false;
                    cbbLoaiBan.Visible = false;
                    break;
                case 1:
                    lblTimbanCFTheoten.Visible = true;
                    txtTenBanCFTim.Visible = true;
                    lblTrangthaibanCFTim.Visible = false;
                    rdbanCFDangban.Visible = false;
                    rdbanCFDangranh.Visible = false;
                    lblChonLoaiBan.Visible = false;
                    rdBillards.Visible = false;
                    rdCafe.Visible = false;
                    cbbLoaiBan.Visible = false;
                    break;
                case 2:
                    lblTimbanCFTheoten.Visible = false;
                    txtTenBanCFTim.Visible = false;
                    lblTrangthaibanCFTim.Visible = true;
                    rdbanCFDangban.Visible = true;
                    rdbanCFDangranh.Visible = true;
                    lblChonLoaiBan.Visible = false;
                    rdBillards.Visible = false;
                    rdCafe.Visible = false;
                    cbbLoaiBan.Visible = false;
                    break;
                
                case 3:
                    lblTimbanCFTheoten.Visible = false;
                    txtTenBanCFTim.Visible = false;
                    lblTrangthaibanCFTim.Visible = false;
                    rdbanCFDangban.Visible = false;
                    rdbanCFDangranh.Visible = false;
                    lblChonLoaiBan.Visible = true;
                    rdBillards.Visible = true;
                    rdCafe.Visible = true;
                    cbbLoaiBan.Visible = false;
                    break;
                case 4:
                    lblTimbanCFTheoten.Visible = false;
                    txtTenBanCFTim.Visible = false;
                    lblTrangthaibanCFTim.Visible = true;
                    rdbanCFDangban.Visible = true;
                    rdbanCFDangranh.Visible = true;
                    lblChonLoaiBan.Visible = false;
                    rdBillards.Visible = false;
                    rdCafe.Visible = false;
                    cbbLoaiBan.Visible = true;
                    break;
            }
        }

        private void btnTimKiemBanCF_Click(object sender, EventArgs e)
        {
            int chon = cbbKieuTimKiemBanCafe.SelectedIndex;
            BanRepository ban = new BanRepository();
            switch (chon)
            {
                case 0:
                    MessageBox.Show("Bạn chưa chọn kiểu tìm kiếm");
                    break;
                case 1:
                    String ten = txtTenBanCFTim.Text;
                    Ban a = ban.GetByTenBan(ten);
                    if (a != null)
                    {
                        LoadBan(ban.DSBanTheoTen(ten));
                    }
                    else
                    {
                        MessageBox.Show("Không có bàn bạn cần tìm");
                    }
                    break;
                case 2:
                    bool tt;
                    if (rdbanCFDangban.Checked)
                    {
                        tt = false;
                    }
                    else
                    {
                        tt = true;
                    }
                    IQueryable<Ban> list = ban.TimbanTheoTT(tt);
                    if (list != null)
                    {
                        LoadBan(list);
                    }
                    else
                    {
                        MessageBox.Show("Không có bàn bạn cần tìm");
                    }
                    break;
              
                case 3:
                    bool t = false;
                    if (rdCafe.Checked)
                    {
                        t = true;
                    }
                    IQueryable<Ban> listds = ban.GetAllTheoLoai(t);
                    if (listds != null)
                    {
                        LoadBan(listds);
                    }
                    else
                    {
                        MessageBox.Show("Không có bàn bạn cần tìm");
                    }

                    break;
                case 4:
                    bool trat;
                    //cbbLoaiBan.SelectedIndex = 0;
                    lvDSBanTimKiem.Items.Clear();
                    if (rdbanCFDangban.Checked)
                    {
                        trat = false;
                    }
                    else
                    {
                        trat = true;
                    }
                    bool loa;
                    if (cbbLoaiBan.Text == "Cafe")
                    {
                        loa = true;
                    }
                    else
                    {
                        
                            loa = false;
                    }
                    IQueryable<Ban> lit = ban.GetAllTheoLoai(loa);
                    if (lit != null)
                    {
                        foreach (Ban asw in lit)
                        {
                            if (asw.TrangThai == trat)
                            {
                                ListViewItem iteamas = new ListViewItem(asw.TenBan);
                                if (asw.TrangThai == true)
                                {
                                    iteamas.SubItems.Add("Đang rảnh");
                                }
                                else
                                {
                                    iteamas.SubItems.Add("đang bận");
                                }
                                //iteamas.SubItems.Add(asw.TrangThai.ToString());
                                iteamas.SubItems.Add(asw.Gia.ToString());
                                if (asw.Cafe == true)
                                {
                                    iteamas.SubItems.Add("Cafe");
                                }
                                else
                                {
                                    iteamas.SubItems.Add("Billards");
                                }
                                //iteamas.SubItems.Add(asw.LoaiBan.ToString());
                                lvDSBanTimKiem.Items.Add(iteamas);
                            }

                        }

                    }
                    
                    //if (list != null)
                    //{
                    //    LoadBan(list);
                    //}
                    break;
            }
        }
        public void LoadBan(IQueryable<Ban> list)
        {
            lvDSBanTimKiem.Items.Clear();
            foreach (Ban b in list)
            {
                ListViewItem item = lvDSBanTimKiem.Items.Add(b.TenBan);
                if (b.TrangThai == true)
                {
                    item.SubItems.Add("Đang rảnh");
                }
                else
                {
                    item.SubItems.Add("đang bận");
                }
                item.SubItems.Add(b.Gia.ToString());
                if (b.Cafe == true)
                {
                    item.SubItems.Add("Cafe");
                }
                else
                {
                    item.SubItems.Add("Billards");
                }
            }
        }
    }
}
